//
//  SpacePanoController.m
//  cutter
//
//  Created by ZhangLi on 16/1/11.
//  Copyright © 2016年 e360. All rights reserved.
//

#import "SpacePanoController.h"
#import <CommonCrypto/CommonDigest.h>
#import "SpaceEditController.h"

@implementation SpacePanoController
-(NSString*)hashRequest:(NSString*)request{
    if (request) {
        const char *cStr = [request UTF8String];
        unsigned char result[CC_MD5_DIGEST_LENGTH];
        CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
        NSMutableString *temp = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
        for(uint i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
            [temp appendFormat:@"%02x",result[i]];
        }
        //
        NSString *src = [temp lowercaseString];
        NSString *ext = [request pathExtension];
        if (ext && [ext length] > 0) {
            return [src stringByAppendingPathExtension:ext];
        }
        return src;
    }
    return @"";
}
-(NSString*)pathForTemporary:(NSString*)path{
    return [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:path];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    [self.view addGestureRecognizer:pan];
    [pan setCancelsTouchesInView:NO];
    //
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@""];
    NSLog(@"%@",path);
    if(self.data){
        NSLog(@"%@",self.data);
         __weak SpacePanoController *blockSelf = self;
        [MBProgressHUD showHUDAddedTo:blockSelf.view animated:YES];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSString *tmp = [AppData getPath:[self.data objectForKey:@"front"]];
            NSURL *url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"front"] delegate:nil cache:2];
            
            tmp = [AppData getPath:[self.data objectForKey:@"right"]];
            url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"right"] delegate:nil cache:2];
            
            tmp = [AppData getPath:[self.data objectForKey:@"left"]];
            url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"left"] delegate:nil cache:2];
            
            tmp = [AppData getPath:[self.data objectForKey:@"back"]];
            url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"back"] delegate:nil cache:2];
            
            tmp = [AppData getPath:[self.data objectForKey:@"down"]];
            url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"down"] delegate:nil cache:2];
            
            tmp = [AppData getPath:[self.data objectForKey:@"up"]];
            url = [NSURL URLWithString:tmp];
            [TMLoader load:[TMRequest requestWithURL:url data:nil] name:[self.data objectForKey:@"up"] delegate:nil cache:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
                NSString *front=pathForLibrary([self.data objectForKey:@"front"]);
                NSString *right=pathForLibrary([self.data objectForKey:@"right"]);
                NSString *left=pathForLibrary([self.data objectForKey:@"left"]);
                NSString *back=pathForLibrary([self.data objectForKey:@"back"]);
                NSString *down=pathForLibrary([self.data objectForKey:@"down"]);
                NSString *up=pathForLibrary([self.data objectForKey:@"up"]);
                
                NSArray *cubeMapFileNames=[NSArray arrayWithObjects:right,left,down,up,front,back,nil];
                [self loadPanorama:cubeMapFileNames atIndex:0];
                //[right,left,down,up,front,back]
                UIView * view;
                id item =[self.data objectForKey:@"point"];
                for(int i=0;i<[item count];i++){
                    float _u=([[[item objectAtIndex:i] objectForKey:@"posx"] floatValue]-300)/300;
                    float _v=([[[item objectAtIndex:i] objectForKey:@"posy"] floatValue]-225)/225;
                    
                    if([[[item objectAtIndex:i] objectForKey:@"viewtype"] isEqualToString:@"front"]){
                        view = [self addHotSpot:[UIImage imageNamed:@"btn_Estate_Label.png"] u:_u v:_v face:4];
                    }
                    if([[[item objectAtIndex:i] objectForKey:@"viewtype"] isEqualToString:@"back"]){
                        view = [self addHotSpot:[UIImage imageNamed:@"btn_Estate_Label.png"] u:_u v:_v face:5];
                    }
                    //
                    if([[[item objectAtIndex:i] objectForKey:@"viewtype"] isEqualToString:@"right"]){
                        view = [self addHotSpot:[UIImage imageNamed:@"btn_Estate_Label.png"] u:_u v:_v face:1];
                    }
                    if([[[item objectAtIndex:i] objectForKey:@"viewtype"] isEqualToString:@"left"]){
                        view = [self addHotSpot:[UIImage imageNamed:@"btn_Estate_Label.png"] u:_u v:_v face:0];
                    }
                    
                    view.tag = 100+i;
                    //            （当强位置－编辑器的一半）／编辑器的一半 600*450
                }
            });
        });
        
        
    }
}


-(void)hotspotTouch:(UIControl*)sender{
    id item =[self.data objectForKey:@"point"];
    NSLog(@"%@",[[item objectAtIndex:sender.tag-100] objectForKey:@"classid"]);
    self.pointID= [[[item objectAtIndex:sender.tag-100] objectForKey:@"classid"] integerValue];
    [self.tempButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"spaceProductDetails"]){
        __weak UINavigationController *root = segue.destinationViewController;
        __weak SpaceHotPointDetail *HotPointDetail = [root.viewControllers firstObject];
        NSLog(@"%@",HotPointDetail);
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
        NSMutableArray *array = [[NSMutableArray alloc]init];
        NSMutableArray *array2 = [[NSMutableArray alloc]init];
        
        id class =nil;
        NSInteger ID=0;
        //
        class = [[NSMutableArray alloc]init];
        class = [self.data objectForKey:@"house_sample_room_class"];
        
        
        for(int i=0;i<[class count];i++){
            if([[[class objectAtIndex:i] objectForKey:@"id"] intValue] == self.pointID)
            {
                ID = i;
                break;
            }
        }
        id tp =  [class objectAtIndex:ID];
        id temp =[tp objectForKey:@"class_product"];
        //
        
        //
        for (NSDictionary * obj in temp) {
            if([[obj objectForKey:@"product"] count]>0){
                NSString *parentid = [[obj objectForKey:@"product"] objectForKey:@"parentid"];
                NSString *hardpacked = [[obj objectForKey:@"product"] objectForKey:@"temp_hardpacked"];
                if(parentid)
                {
                    if([hardpacked intValue]==0){
                        if (dic[parentid] == nil) {
                            NSMutableArray *arr = [NSMutableArray array];
                            [arr addObject:obj];
                            dic[parentid] = arr;
                        }
                        else
                        {
                            NSMutableArray *arr = dic[parentid];
                            [arr addObject:obj];
                        }
                    }
                    if([hardpacked intValue]==1){
                        if (dic2[parentid] == nil) {
                            NSMutableArray *arr = [NSMutableArray array];
                            [arr addObject:obj];
                            dic2[parentid] = arr;
                        }
                        else
                        {
                            NSMutableArray *arr = dic2[parentid];
                            [arr addObject:obj];
                        }
                    }
                }
                
                //
            }
            
        }
        if([dic count]>0){
            for (id key in dic) {
                [array addObject:dic[key]];
            }
        }
        if([dic2 count]>0){
            for (id key in dic2) {
                [array2 addObject:dic2[key]];
            }
        }
        
        if([array count]>0){
            NSIndexPath *indexPath=(NSIndexPath *)[self.DicIndexPath2 objectForKey:[NSNumber numberWithInteger:ID]];
            NSMutableArray *data =[NSMutableArray arrayWithObject:[[array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
            [HotPointDetail setData:[data firstObject]];
            
            
        }else{
            NSIndexPath *indexPath=(NSIndexPath *)[self.DicIndexPath objectForKey:[NSNumber numberWithInteger:0]];
            if([array2 count]>0){
                NSMutableArray *data =[NSMutableArray arrayWithObject:[[array2 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
                if([data count]>0){
                    [HotPointDetail setData:[data firstObject]];
                }
                
            }
            
            
        }
        return;
        
        
        
        
//        NSInteger ID=0;
//        for(int i=0;i<[[self.data objectForKey:@"house_sample_room_class"] count];i++){
//            if([[[[self.data objectForKey:@"house_sample_room_class"] objectAtIndex:i] objectForKey:@"id"] intValue] == self.pointID)
//            {
//                ID = i;
//                break;
//            }
//        }
//        id tp =  [[self.data objectForKey:@"house_sample_room_class"] objectAtIndex:ID];
//        id temp =[tp objectForKey:@"class_product"];
//        for(int i=0;i<[temp count];i++)
//        {
//            id obj = [temp objectAtIndex:i];
//            NSString *hardpacked = [[obj objectForKey:@"product"] objectForKey:@"temp_hardpacked"];
//            if([hardpacked intValue]==0){
//
//                //
//                if([[obj objectForKey:@"classid"] intValue] == self.pointID)
//                {
//                    [HotPointDetail setData:obj];
//                    break;
//                }
//            }else{
//                
//                if([[obj objectForKey:@"classid"] intValue])
//                {
//                    [HotPointDetail setData:obj];
//                    break;
//                }
//            }
//        }
//        
//        return;
        
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
//        NSMutableArray *array = [[NSMutableArray alloc]init];
//        NSMutableArray *array2 = [[NSMutableArray alloc]init];
//        NSInteger ID=0;
//        for(int i=0;i<[[self.data objectForKey:@"house_sample_room_class"] count];i++){
//            if([[[[self.data objectForKey:@"house_sample_room_class"] objectAtIndex:i] objectForKey:@"id"] intValue] == self.pointID)
//            {
//                ID = i;
//                break;
//            }
//        }
//        id tp =  [[self.data objectForKey:@"house_sample_room_class"] objectAtIndex:ID];
//        id temp =[tp objectForKey:@"class_product"];
//        for (NSDictionary * obj in temp) {
//            if([[obj objectForKey:@"product"] count]>0){
//                NSString *parentid = [[obj objectForKey:@"product"] objectForKey:@"parentid"];
//                NSString *hardpacked = [[obj objectForKey:@"product"] objectForKey:@"temp_hardpacked"];
//                if(parentid)
//                {
//                    if([hardpacked intValue]==0){
//                        if (dic[parentid] == nil) {
//                            NSMutableArray *arr = [NSMutableArray array];
//                            [arr addObject:obj];
//                            dic[parentid] = arr;
//                        }
//                        else
//                        {
//                            NSMutableArray *arr = dic[parentid];
//                            [arr addObject:obj];
//                        }
//                    }
//                    if([hardpacked intValue]==1){
//                        if (dic2[parentid] == nil) {
//                            NSMutableArray *arr = [NSMutableArray array];
//                            [arr addObject:obj];
//                            dic2[parentid] = arr;
//                        }
//                        else
//                        {
//                            NSMutableArray *arr = dic2[parentid];
//                            [arr addObject:obj];
//                        }
//                    }
//                }
//                
//                //
//            }
//            
//        }
//        if([dic count]>0){
//            for (id key in dic) {
//                [array addObject:dic[key]];
//            }
//        }
//        if([dic2 count]>0){
//            for (id key in dic2) {
//                [array2 addObject:dic2[key]];
//            }
//        }
//        
//        
//        //
//        NSLog(@"%@",array);
//        NSLog(@"%@",array2);
//        if([array count]>0)
//        {
//            for(int i=0;i<[array count];i++)
//            {
//                if([[[[array objectAtIndex:i] objectAtIndex:0] objectForKey:@"classid"] intValue] == self.pointID)
//                {
//                    NSMutableArray *data =[NSMutableArray arrayWithObject:[[array objectAtIndex:i] objectAtIndex:0]];
//                    [HotPointDetail setData:data];
//                }
//            }
//        }
//        
//        if([array2 count]>0)
//        {
//            for(int i=0;i<[array2 count];i++)
//            {
//                if([[[[array2 objectAtIndex:i] objectAtIndex:0]objectForKey:@"classid"] intValue] == self.pointID)
//                {
//                    NSMutableArray *data =[NSMutableArray arrayWithObject:[[array2 objectAtIndex:i] objectAtIndex:0]];
//                    [HotPointDetail setData:data];
//                }
//            }
//        }
        
        
        return;
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView beginAnimations:nil context:nil];
    self.tips.hidden = YES;
    [UIView commitAnimations];
}
- (IBAction)backTouch:(id)sender {
    if(self.navigationController){
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
